<template>
  <div class="order-container">
    <el-tabs v-model="activeName" @tab-change="changeActive">
      <!-- tab切换 -->
      <el-tab-pane v-for="(item,index) in tabTypes" :key="index" :label="item.label" :name="item.name"/>
      <div class="holder-container" v-if="orderList.length === 0">
        <el-empty description="暂无订单数据"/>
      </div>
      <div v-else>
        <!-- 订单列表 -->
        <div class="order-item" v-for="(order,index) in orderList" :key="index">
          <div class="head">
            <span>下单时间：{{ order.createTime }}</span>
            <span>订单编号：{{ order.orderId }}</span>
          </div>
          <div class="body">
            <div class="column goods">
              <ul>
                <li v-for="item in order.skusDetailList" :key="item.id">
                  <a href="javascript:;">
                    <img :src="item.goodsImage" alt="" style="min-width: 50px;height: 50px"/>
                  </a>
                  <div class="info">
                    <p class="name ellipsis-2">
                      {{ item.goodsName }}
                    </p>
                    <p class="attr ellipsis">
                      <span>{{ item.attrsText }}</span>
                    </p>
                  </div>
                  <div class="price">¥{{ item.skuTotalPrice?.toFixed(2) }}</div>
                  <div class="count">x{{ item.quantity }}</div>
                </li>
              </ul>
            </div>
            <div class="column state">
              <p>{{ order.orderStatusMap["statusName"] }}</p>
              <p v-if="order.orderStatusMap['status']=== '3'">
                <a href="javascript:;" class="green">查看物流</a>
              </p>
              <p v-if="order.orderStatusMap['status'] === '4'">
                <a href="javascript:;" class="green">评价商品</a>
              </p>
              <p v-if="order.orderStatusMap['status'] === '4'">
                <a href="javascript:;" class="green">查看评价</a>
              </p>
              <p v-if="order.orderStatusMap['status'] === '0'">
                <a href="javascript:;" class="green">已取消</a>
              </p>
            </div>
            <div class="column amount">
              <p class="red">¥{{ order.totalPrice?.toFixed(2) }}</p>
              <p>（含运费：¥{{ order.postage?.toFixed(2) }}）</p>
              <p>在线支付</p>
            </div>
            <div class="column action">
              <el-button v-if="order.orderStatusMap['status'] === '2'" type="primary"
                         size="small" @click="toSend(order.orderId)">
                发货
              </el-button>
            </div>
          </div>
        </div>
        <!-- 分页 -->
        <pagination :total="total" @pagination="getPage" :limit="params.pageSize"/>
      </div>
    </el-tabs>
  </div>
</template>

<script setup>
import {onMounted, ref} from "vue";
import {getOrderByStatusAPI, toSendGoodsAPI} from "@/api/orderManage.js";
import Pagination from "@/components/Pagination/index.vue";
import {ElMessage} from "element-plus";

const tabTypes = [
  {name: -1, label: "全部订单"},
  {name: 1, label: "待付款"},
  {name: 2, label: "待发货"},
  {name: 3, label: "待收货"},
  {name: 4, label: "待评价"},
  {name: 5, label: "已完成"},
  {name: 0, label: "已取消"}
]
const activeName = ref(-1)
const orderList = ref([])
const total = ref(0)
const params = ref({
  pageSize: 5,
  current: 1,
  orderStatus: -1
})

const getPage = (res) => {
  params.value.current = res.page
  params.value.pageSize = res.limit
  getOrderList()
}
const toSend = async (orderId) => {
  console.log("orderId",orderId)
  const res = await toSendGoodsAPI(orderId)
  if(res.data){
    ElMessage.success("发货成功")
    await getOrderList()
  }
}
const getOrderList = async () => {
  const res = await getOrderByStatusAPI(params.value)
  orderList.value = res.data.records
  total.value = res.data.total
}
onMounted(() => getOrderList())
const changeActive = () => {
  params.value.orderStatus = activeName.value
  getOrderList()
}

</script>

<style scoped lang="scss">
.order-container {
  margin-left: 5px;
  background: #fafcff;
.pagination-container {
  margin-left: 5px;
  justify-content: center;
}

.holder-container {
  min-height: 500px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.order-item {
  background: #fafcff;
  margin-left: 4px;
  border: 1px solid #f5f5f5;

  .head {
    height: 50px;
    line-height: 50px;
    background: #fafcff;
    padding: 0 20px;
    overflow: hidden;

    span {
      margin-right: 20px;

      &.down-time {
        margin-right: 0;
        float: right;

        i {
          vertical-align: middle;
          margin-right: 3px;
        }

        b {
          vertical-align: middle;
          font-weight: normal;
        }
      }
    }

    .del {
      margin-right: 0;
      float: right;
      color: #999;
    }
  }

  .body {
    display: flex;
    align-items: stretch;

    .column {
      border-left: 1px solid #fafcff;
      text-align: center;
      padding: 20px;

      > p {
        padding-top: 10px;
      }

      &:first-child {
        border-left: none;
      }

      &.goods {
        flex: 1;
        padding: 0;
        align-self: center;

        ul {
          li {
            border-bottom: 1px solid #fafcff;
            padding: 10px;
            display: flex;

            &:last-child {
              border-bottom: none;
            }

            .image {
              width: 10px;
              height: 10px;
              border: 1px solid #fafcff;
            }

            .info {
              width: 220px;
              text-align: left;
              padding: 0 10px;

              p {
                margin-bottom: 5px;

                &.name {
                  height: 38px;
                }

                &.attr {
                  color: #999;
                  font-size: 12px;

                  span {
                    margin-right: 5px;
                  }
                }
              }
            }

            .price {
              width: 100px;
            }

            .count {
              width: 80px;
            }
          }
        }
      }

      &.state {
        width: 120px;

        .green {
          color: #27ba9b;
        }
      }

      &.amount {
        width: 200px;

        .red {
          color: #cf4444;
        }
      }

      &.action {
        width: 140px;

        a {
          display: block;

          &:hover {
            color: #27ba9b;
          }
        }
      }
    }
  }
}
}
</style>
